package com.vipboot.framework.core.domain;

import com.vipboot.framework.core.enums.DataType;
import com.vipboot.framework.core.errorcode.SystemErrorCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

@NoArgsConstructor
@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    public static final int OkCode = 200;
    public static final String OkMsg = "操作成功";

    private Integer code;
    private String msg;
    private T data;
    private Integer dataType;

    public R(Integer code, String msg) {
        this(code, msg, null);
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.dataType = DataType.Normal.getValue();
    }

    public static <T> R<T> ok() {
        return new R<>(OkCode, OkMsg);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(OkCode, OkMsg, data);
    }

    // -------------------------------------------- 错误码 --------------------------------------------
    public static <T> R<T> fail() {
        return new R<>(SystemErrorCode.SystemError.getCode(), SystemErrorCode.SystemError.getMsg(), null);
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(SystemErrorCode.SystemError.getCode(), msg, null);
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return new R<>(code, msg, null);
    }

}
